package cn.edu.xust.communication.util;




import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class TimeStampUtil {

    static SimpleDateFormat formatter = new  SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    public static Timestamp  longToTimeStamp(Long  time){
        Timestamp timestamp = null;
        if (time == null ){
            return null ;
        }else {
            timestamp=new Timestamp(time);
        }
        return timestamp;
    }

    public static Long  timeStampToLong(Timestamp  timestamp){
        Long  time=timestamp.getTime();
        return time;
    }

    public static Timestamp getCurrentTime(){
        Timestamp timestamp=Timestamp.valueOf( LocalDateTime.now());
        return timestamp;
    }

    //减一小时 一天  一月  一年
    public static Timestamp timeSubByTimeType(Timestamp  timestamp,String  timeType,Integer num){
        if(TimeEnums.HOUR.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().minusHours(num));  //减一天
        }else if(TimeEnums.DAY.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().minusDays(num));  //减一天
        }else if(TimeEnums.MONTH.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().minusMonths(num)); //减一月
        }else if(TimeEnums.YEAR.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().minusYears(num)); //减一年
        }
        return timestamp ;
    }

    //加一小时 一天  一月  一年
    public static Timestamp timeAddByTimeType(Timestamp  timestamp,String  timeType,Integer num){
        if(TimeEnums.HOUR.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().plusHours(num));
        }else if(TimeEnums.DAY.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().plusDays(num));
        }else if(TimeEnums.MONTH.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().plusMonths(num));
        }else if(TimeEnums.YEAR.getType() == timeType){
            timestamp = Timestamp.valueOf(timestamp.toLocalDateTime().plusYears(num));
        }
        return timestamp ;
    }

    public static Timestamp getFirstOfTime(Timestamp timestamp, String timeType){
        if(TimeEnums.DAY.getType() == timeType){
            timestamp =Timestamp.valueOf(LocalDateTime.of(timestamp.toLocalDateTime().toLocalDate(),LocalTime.MIN));                              //当天凌晨
        }else if(TimeEnums.MONTH.getType() == timeType){
            timestamp =Timestamp.valueOf(timestamp.toLocalDateTime().with(TemporalAdjusters.firstDayOfMonth())); //当月第一天
        }
        return timestamp ;
    }

    //判断是不是今年
    public static Boolean timeIsThisYear(Timestamp  timestamp){
        if(timestamp.toLocalDateTime().getYear() == LocalDateTime.now().getYear()){
            return true;
        }
    return false;
    }

    //判断是不是今月
    public static Boolean timeIsThisMonth(Timestamp  timestamp){
        LocalDateTime    time =timestamp.toLocalDateTime();
        LocalDateTime    localDateTime=LocalDateTime.now();
        if(timeIsThisYear(timestamp)==true && time.getMonthValue() == localDateTime.getMonthValue()){
            return true;
        }
        return false;
    }

    public static String getYearMonth(Timestamp  timestamp){
         LocalDateTime localDateTime = timestamp.toLocalDateTime();
         String yearMonth =String.valueOf(localDateTime.getYear()) ;
         int    month = localDateTime.getMonthValue();
         if(month < 10){
             yearMonth += "0"+month ;
         }else {
             yearMonth += month;
         }
         return yearMonth;
    }

    //判断是不是今天
    public static Boolean timeIsToday(Timestamp  timestamp){
        LocalDateTime    time =timestamp.toLocalDateTime();
        LocalDateTime    localDateTime=LocalDateTime.now();
        if(timeIsThisMonth(timestamp) && localDateTime.getDayOfMonth() == time.getDayOfMonth()){
            return true;
        }
        return false;
    }
    public static String timeToDate(Long timestamp) {
        return formatter.format(new Date(timestamp));

    }

    public static String getDate(Timestamp timestamp){
       return timestamp.toLocalDateTime().toLocalDate().toString();

    }

    public static String getDate(Long timestamp){
        return getDate(new Timestamp(timestamp));

    }


    public static void main(String[] args)
    {

    }
}
